<?php
/**
 * create by phpstorm
 * User: 冰帝
 * Date: 2023/3/19
 * Remark: 鸡圈接口
 * 现在的努力是为了小时候吹过的牛逼
 */

namespace app\admin\controller;

use app\common\model\GoodsCategory;
use think\facade\Db;
use think\facade\View;

class House extends AdminBase
{
    public function  index(){
        $model=Db::name('house')
            ->where(['special'=>1])
            ->order(['status'=>'asc','id'=>'desc'])
            ->column('id,name,status,create_time');
        foreach ($model as $k=>$v){
            $model[$k]['num']=20-Db::name('zoon')
                ->where(['house_id'=>$v['id'],'status'=>[2,3,4]])
                ->count();
            $model[$k]['create_time']=date("Y-m-d H:i:s",$v['create_time']);
            $model[$k]['status_name']=\app\common\model\House::STATUS[$v['status']];
        }
        return View::fetch("index",['model'=>$model]);
    }
    public function  special_index(){
        $model=Db::name('house')
            ->where(['special'=>2])
            ->order(['status'=>'asc','id'=>'desc'])
            ->column('id,name,status,pay_time,valid_time,user_id,shiji_price');
        foreach ($model as $k=>$v){
            $model[$k]['num']=Db::name('zoon')
                    ->where(['house_id'=>$v['id'],'status'=>[2,3,4]])
                    ->count();
            $model[$k]['pay_time']=date("Y-m-d H:i:s",$v['pay_time']);
            $model[$k]['valid_time']=date("Y-m-d H:i:s",$v['valid_time']);
            $model[$k]['status_name']=\app\common\model\House::STATUS[$v['status']];
            $model[$k]['nickname']=Db::name('user')
                ->where('id',$v['user_id'])
                ->find()['nickname'];
        }
        return View::fetch("special_index",['model'=>$model]);
    }
    public function add(){
        if (request()->isPost()) {
            $data = input('post.');
            if ($data['id']) {
                $data['update_time']=time();
                $res = Db::name('house')
                    ->where('id', $data['id'])
                    ->update($data);
            } else {
                $data['create_time']=time();
                $res = Db::name('house')
                    ->insert($data);
            }
            if (!$res) {
                return show(0, '数据保存失败');
            }
            return show(1, '数据保存成功');
        } else {
            $id = input('id');
            $model= Db::name('house')
                ->where('id', $id)
                ->find();
            return View::fetch('add', ['model' =>$model]);
        }
    }
}